//
//  ComposeToolbar.m
//  微博2期
//
//  Created by J on 15/12/8.
//
//

#import "ComposeToolbar.h"
@interface ComposeToolbar()

@property(nonatomic,weak)UIButton* emotionButton;
@end
@implementation ComposeToolbar




- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        
        [self setupBtn:@"compose_camerabutton_background" highImage:@"compose_camerabutton_background_highlighted" tag:ComposeToolbarDelegateButtonCamera];
        [self setupBtn:@"compose_toolbar_picture" highImage:@"compose_toolbar_picture_highlighted" tag:ComposeToolbarDelegateButtonPicture];
        [self setupBtn:@"compose_mentionbutton_background" highImage:@"compose_mentionbutton_background_highlighted" tag:ComposeToolbarDelegateButtonMention];
        [self setupBtn:@"compose_trendbutton_background" highImage:@"compose_trendbutton_background_highlighted" tag:ComposeToolbarDelegateButtonTrend];
     self.emotionButton =   [self setupBtn:@"compose_emoticonbutton_background" highImage:@"compose_emoticonbutton_background_highlighted" tag:ComposeToolbarDelegateButtonEmotion];

    }
    
    return self;
}

- (void)setShowEmotionButton:(BOOL)showEmotionButton{
    
    _showEmotionButton = showEmotionButton;
    
    
    NSString* image =  @"compose_keyboardbutton_background";
    NSString* hightImage =  @"compose_keyboardbutton_background_highlighted";

    if (showEmotionButton) {//显示表情图标
        image =  @"compose_emoticonbutton_background";
        hightImage =  @"compose_emoticonbutton_background_highlighted";
    }
    //设置图片
    [self.emotionButton setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [self.emotionButton setImage:[UIImage imageNamed:hightImage] forState:UIControlStateHighlighted];
    

    
}

/**
 创建一个按钮
 */
- (UIButton*)setupBtn:(NSString*)image highImage:(NSString*)highImage tag:(ComposeToolbarDelegateButtonType)type{
    
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = type;
    [self addSubview:button];
    return button;
}

- (void)btnClick:(UIButton*)button{
    
    
    
    if ([self.delegate respondsToSelector:@selector(compostToolbar:didClickButtton:)]) {
        
//        NSUInteger index = (NSUInteger)(button.x/button.width);
        [self.delegate compostToolbar:self didClickButtton:button.tag];
    }
    
}

- (void)layoutSubviews{
    [super layoutSubviews];
    //设置所有按钮frame
    NSUInteger  count = self.subviews.count;
    CGFloat btnW = self.width/count;
    CGFloat btnH = self.height;

    for (NSUInteger i=0; i<count; i++) {
        UIButton * button =self.subviews[i];
        button.y = 0;
        button.x  = i*button.width;
        button.width = btnW;
        button.height = btnH;

    }
    
    
    
}

@end
